package com.chatdating.service.impl;

import com.chatdating.entity.Session;
import com.chatdating.entity.User;
import com.chatdating.entity.dto.MessageSendDto;
import com.chatdating.entity.pojo.ChatMessages;
import com.chatdating.entity.pojo.SendMessage;
import com.chatdating.mapper.ChatMessageMapper;
import com.chatdating.mapper.UserMapper;
import com.chatdating.service.ChatMessageService;
import com.chatdating.websocket.ChannelContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class chatMessageServiceImpl implements ChatMessageService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ChatMessageMapper chatMessageMapper;
    @Resource
    private ChannelContextUtils channelContextUtils;

    @Override
    public void saveMessage(SendMessage sendMessage,String username) {

        //1 更新会话消息
        //获取到要更改的 sessionid
        String sessionId = sendMessage.getSessionId();
        //最后一条消息
        String messageContent = sendMessage.getMessageContent();
        //最后一条时间
        String sendTime = sendMessage.getSendTime();

        Session session  = new Session();
        session.setLastMessage(messageContent);
        session.setSessionId(sessionId);
        session.setLastTime(sendTime);
        userMapper.updateSession(session);

        User byName = userMapper.findByName(username);
        int i = userMapper.queryContactBySessionAndUserId(sessionId, username);
        String reseiveName = userMapper.getNameById(i);
        String sreceivename = String.valueOf(i);

        //更新聊天记录表
       ChatMessages chatMessages = new ChatMessages();
       chatMessages.setMessageContent(messageContent);
       chatMessages.setStatus(1);
       chatMessages.setSendTime(Long.valueOf(sendTime));
       chatMessages.setSendUserNickName(username);
       chatMessages.setSendUserId(String.valueOf(byName.getId()));
       chatMessages.setContactId(sreceivename);
       chatMessages.setSessionId(sessionId);
       int key = chatMessageMapper.insert(chatMessages);
        Long messageId = chatMessages.getMessageId();

        //构造发送给另一个用户的消息
        MessageSendDto messageSendDto = new MessageSendDto();
        //消息id
        messageSendDto.setMessageId(messageId);

       //联系人id
        messageSendDto.setContactId(String.valueOf(i));
       //发送时间
       messageSendDto.setSendTime(Long.valueOf(sendTime));
       //发送人昵称
       messageSendDto.setSendUserNickName(username);

        //封装聊天消息
       SendMessage sendMessages = new SendMessage();
        //最后一条消息
       sendMessages.setMessageContent(messageContent);
       //联系人名称
       sendMessages.setSenderId(sendMessage.getSenderId());
       //
        sendMessages.setSessionId(sessionId);
        sendMessages.setMessageId(messageId.toString());
        sendMessages.setSendTime(sendTime);


       messageSendDto.setSendMessage(sendMessages);

       channelContextUtils.sendMsg(messageSendDto,reseiveName);
    }
}
